package org.example.entity;

import java.io.FileWriter;
import java.io.IOException;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Random;

/**
 * @author TonySong
 * 电影票：父类
 * @date 2025/7/28 0028
 * @time 16:27
 */
public class Ticket implements Serializable {
    private Movie movie;
    private String seat;

    public Movie getMovie() {
        return movie;
    }

    public void setMovie(Movie movie) {
        this.movie = movie;
    }

    public int calc(){
        //普通票的价格：原价
        return this.movie.getPrice();
    }

    public String getSeat() {
        return seat;
    }

    public Ticket(Movie movie) {
        this.movie = movie;
        this.seat = createSeat();
    }

    public String createSeat(){
        Random random = new Random();
        return (random.nextInt(10)+1)+"排"+(random.nextInt(10)+1)+"座";
    }

    public void printTicket(){
        //将票信息打印到txt文件中
        String filename = "ticket_" + System.currentTimeMillis() + ".txt";
        try (FileWriter writer = new FileWriter(filename)) {
            writer.write("========== 电影票 ==========\n");
            writer.write("电影名称: " + movie.getName() + "\n");
            writer.write("放映时间: " + movie.getPlayTime() + "\n");
            writer.write("座位号: " + seat + "\n");
            writer.write("票价: ¥" + calc() + "\n");
            writer.write("购票时间: " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) + "\n");
            writer.write("========================\n");
            System.out.println("电影票已打印到文件: " + filename);
        } catch (IOException e) {
            System.err.println("打印电影票时出错: " + e.getMessage());
        }
    }
    public void showTicket(){
        System.out.println("========== 电影票 ==========");
        System.out.println("电影名称: " + movie.getName());
        System.out.println("放映时间: " + movie.getPlayTime());
        System.out.println("座位号: " + seat);
        System.out.println("票价: ¥" + calc());
        System.out.println("购票时间: " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        System.out.println("========================");
    }
}
